package com.fpx.open.platform.domain.label;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;

import com.fpx.xms.api.base.util.ExtPropertyConfigurer;
import com.fpx.xms.api.base.util.JsonUtils;

/**
 * 订单标签格式枚举类.
 * @author yangpo
 * @date 2015年8月12日
 */
public enum OrderLabelFormatEnum {

    HTML("html","createHtml.do"), IMG("img","labelImg.do"),PDF("pdf","pdfPrint.do"),PDF_BASE64("pdf_base64","pdfPrint.do");

    /**
     * 模版类型
     */
    private String type;
    
    private String pageUrl;

    private OrderLabelFormatEnum(String type,String pageUrl) {
        this.type = type;
        this.pageUrl = pageUrl;
    };
    
    public static String getSendUrl(String orderLabelFormatStr){
        String returnPageUrl = null;
        String configUrl = ExtPropertyConfigurer.get("api.orderLabelUrl");
        if(null == orderLabelFormatStr){
            return null;
        }
        for (OrderLabelFormatEnum tempEnum : OrderLabelFormatEnum.values()) {
            if (StringUtils.equals(orderLabelFormatStr, tempEnum.type)) {
                returnPageUrl = configUrl+tempEnum.pageUrl;
                break;
            }
        }

        return returnPageUrl;
    }
    
    public static void getData(String type,String data, LabelResponse response) throws Exception{
        if(StringUtils.isBlank(type)||StringUtils.isBlank(data)){
            return ;
        }
        List<String> dataList = new ArrayList<String>();
        if(StringUtils.equals(OrderLabelFormatEnum.HTML.type,type)){
            OrderLabelHtmlResponse html = JsonUtils.fromJson(data, OrderLabelHtmlResponse.class);
            dataList.add(html.getHtmlUrl());
        }
        if(StringUtils.equals(OrderLabelFormatEnum.IMG.type,type)){
            OrderLabelImgResponse img = JsonUtils.fromJson(data, OrderLabelImgResponse.class);
            String [] urls = img.getImgUrl();
            if(null!=urls&&urls.length>0){
                for(String url:urls){
                    dataList.add(url);
                }
            }
        }
        if(StringUtils.equals(OrderLabelFormatEnum.PDF.type,type)){
            OrderLabelPdfResponse pdf = JsonUtils.fromJson(data, OrderLabelPdfResponse.class);
            dataList.add(pdf.getPdfUrl());
        }
        if(StringUtils.equals(OrderLabelFormatEnum.PDF_BASE64.type, type)){
            OrderLabelPdfResponse pdf = JsonUtils.fromJson(data, OrderLabelPdfResponse.class);
            String url = pdf.getPdfUrl();
            HttpClient client = HttpClients.createDefault();
            HttpGet httpget = new HttpGet(url);  
            HttpResponse httpResponse = client.execute(httpget);
            HttpEntity entity = httpResponse.getEntity();  
            InputStream is = entity.getContent();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] bytes = null;
            try {
                int ch = 0;
                while ((ch = is.read()) != -1) {
                    System.out.println(ch);
                    out.write(ch);
                }
                bytes = out.toByteArray();
            } catch (IOException e) {
                throw new Exception(e);
            }finally{
                try{
                    is.close();
                }catch(IOException e){
                    throw new Exception(e);
                }
                try{
                    out.close();
                }catch(IOException e){
                    throw new Exception(e);
                }
            }
            String baseData = Base64.encodeBase64String(bytes);
            dataList.add(baseData);
        }
        
        response.setDatas(dataList);
    }
}
